package org.jeecg.modules.simulator.model.line.station.strategy;

import org.jeecg.modules.simulator.model.line.station.station.Station;
import org.jeecg.modules.simulator.model.line.station.station.TransferStation;
import org.jeecg.modules.simulator.model.person.Person;
import org.jeecg.modules.simulator.model.person.path.PersonPathItem;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: simulator-b20240918
 * @description:
 * @author: renzhg
 * @create: 2024-10-04 11:40
 **/
public class NormalTransferStageImpl implements TransferStrategy {
    @Override
    public void transferStation(List<Person> pf, Map<String, Station> stationNameMap , TransferStation tstation) {
        List<Person> transferPersonList = getTransferPersons(pf, tstation);
        Map<String, List<Person>> groupedByCategory = groupPersonsByNextDirection(transferPersonList, tstation);
        updateTransferPersonsMap(tstation, groupedByCategory);
        removeTransferredPersons(pf, transferPersonList);
        //region 旧代码
//        List<Person> transferPersonList=new ArrayList<>();
//        pf.forEach(p->{
//            String snStionName=p.getPersonPath().getNextDirection(tstation.getName());
//            PersonPathItem personThisPathItem=p.getPersonPath().getPersonPathItemMap().get(tstation.getName());
//            PersonPathItem personNextPathItem=p.getPersonPath().getPersonPathItemMap().get(snStionName);
//            String thisLineId = personThisPathItem.getLineId();
//            String nextLineId = personNextPathItem.getLineId();
//            if(tstation.getTransferStationMap().containsKey(thisLineId)&&tstation.getTransferStationMap().containsKey(nextLineId)){
//                int transTime = tstation.getTransferTimeMap().get(thisLineId+"-"+nextLineId);
//                p.setTransferWalkTime(transTime);
//                transferPersonList.add(p);
//            }
//        });
//        Map<String, List<Person>> groupedByCategory = transferPersonList.stream()
//                .collect(Collectors.groupingBy(p -> p.getPersonPath().getNextDirection(tstation.getName())));
//        tstation.getTransferPersonsMap().putAll(groupedByCategory);
//        transferPersonList.forEach(p->{
//            pf.remove(p);
//
//        });
        //endregion

    }
    /**
     * @description: 获取需要转乘的人员列表
     *
     * @author:  renzhg
     * @date:  2024/9/23 17:26
     **/
    private List<Person> getTransferPersons(List<Person> pf, TransferStation tstation) {
        List<Person> transferPersonList = new ArrayList<>();
        pf.forEach(p -> {
            String snStationName = p.getPersonPath().getNextDirection(tstation.getName());
            PersonPathItem personThisPathItem = p.getPersonPath().getPersonPathItemMap().get(tstation.getName());
            PersonPathItem personNextPathItem = p.getPersonPath().getPersonPathItemMap().get(snStationName);

            if (shouldTransfer(tstation, personThisPathItem, personNextPathItem)) {
                int transTime = tstation.getTransferTimeMap().get(personThisPathItem.getLineId() + "-" + personNextPathItem.getLineId());
                p.setTransferWalkTime(transTime);
                transferPersonList.add(p);
            }
        });
        return transferPersonList;
    }
    /**
     * @description: 判断当前人员是否需要转乘
     *
     * @author:  renzhg
     * @date:  2024/9/23 17:26
     **/
    private boolean shouldTransfer(TransferStation tstation, PersonPathItem personThisPathItem, PersonPathItem personNextPathItem) {
        String thisLineId = personThisPathItem.getLineId();
        String nextLineId = personNextPathItem.getLineId();
        return tstation.getTransferStationMap().containsKey(thisLineId) && tstation.getTransferStationMap().containsKey(nextLineId);
    }
    /**
     * @description: 将转乘人员按下一站的方向分组。
     *
     * @author:  renzhg
     * @date:  2024/9/23 17:27
     **/
    private Map<String, List<Person>> groupPersonsByNextDirection(List<Person> transferPersonList, TransferStation tstation) {
        return transferPersonList.stream()
                .collect(Collectors.groupingBy(p -> p.getPersonPath().getNextDirection(tstation.getName())));
    }
    /**
     * @description: 更新转乘人员集合。
     *
     * @author:  renzhg
     * @date:  2024/9/23 17:27
     **/
    private void updateTransferPersonsMap(TransferStation tstation, Map<String, List<Person>> groupedByCategory) {
        tstation.getTransferPersonsMap().putAll(groupedByCategory);
    }
    /**
     * @description: 从原始人员列表中移除已转乘的人员
     *
     * @author:  renzhg
     * @date:  2024/9/23 17:28
     **/
    private void removeTransferredPersons(List<Person> pf, List<Person> transferPersonList) {
        pf.removeAll(transferPersonList); // 使用 removeAll 一次性移除
    }
}
